package com.pandora.common.util;

import java.security.MessageDigest;

import com.pandora.framework.util.LogUtils;

/**
 * MD5工具类
 * 
 * @author JingChenglong 2017/12/20 12:55
 *
 */
public class MD5Utils {

	private static final String ALGORITHM = "MD5";
	private static final String CHARSER = "UTF-8";

	/**
	 * MD5 加密给定字符串
	 * 
	 * @param str
	 * @return
	 */
	public static String md5(String str) {

		return md5(str, CHARSER);
	}

	/**
	 * MD5加密
	 * 
	 * @param str
	 * @param charset
	 * @return
	 */
	public static String md5(String str, String charset) {

		try {
			MessageDigest md5 = MessageDigest.getInstance(ALGORITHM);
			byte md5Bytes[] = md5.digest(str.getBytes(charset));
			return StringUtils.byte2hex(md5Bytes);
		} catch (Exception e) {
			LogUtils.logError(
					(new StringBuilder(String.valueOf(CommonUtils.getTraceInfo()))).append(e.getMessage()).toString());
		}

		return str;
	}
}